home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / CHECKER3.C < prev    next >
Text File  |  1990-11-28  |  5KB  |  200 lines

  1. /*   Checker2.C   -  Mouse Hit Program with keyboard interface
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6.  
  7. #define DIVISIONS 5
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  10.  
  11. int PASCAL WinMain (HANDLE hInstance,
  12.                     HANDLE hPrevInstance,
  13.                     LPSTR  lpszCmdParam,
  14.                     int    nCmdShow)
  15.                     
  16.   {
  17.   static char szAppName[] = "Checker2";
  18.   HWND        hwnd;
  19.   MSG         msg;
  20.   WNDCLASS    wndclass;
  21.   
  22.   if (!hPrevInstance)
  23.      {
  24.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  25.      wndclass.lpfnWndProc      = WndProc;
  26.      wndclass.cbClsExtra       = 0;
  27.      wndclass.cbWndExtra       = 0;
  28.      wndclass.hInstance        = hInstance;
  29.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  30.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  31.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  32.      wndclass.lpszMenuName     = NULL;
  33.      wndclass.lpszClassName    = szAppName;
  34.      
  35.      RegisterClass(&wndclass);
  36.      }
  37.      
  38.   hwnd = CreateWindow (szAppName,
  39.                        "Checker2 Mouse Hit-Test Demo",
  40.                        WS_OVERLAPPEDWINDOW, 
  41.                        CW_USEDEFAULT,   
  42.                        CW_USEDEFAULT,
  43.                        CW_USEDEFAULT,   
  44.                        CW_USEDEFAULT,   
  45.                        NULL,
  46.                        NULL,
  47.                        hInstance,
  48.                        NULL);
  49.                        
  50.   ShowWindow (hwnd, nCmdShow);
  51.   UpdateWindow (hwnd);
  52.   
  53.   while (GetMessage (&msg, NULL, 0, 0))
  54.     {
  55.     TranslateMessage (&msg);
  56.     DispatchMessage  (&msg);
  57.     }
  58.     
  59.   return msg.wParam;
  60.   }
  61.   
  62. long FAR PASCAL WndProc (HWND hwnd,
  63.                          WORD message,
  64.                          WORD wParam,
  65.                          LONG lParam)
  66.                          
  67.   {
  68.   static BOOL fState[DIVISIONS][DIVISIONS];
  69.   static short cxBlock, cyBlock;
  70.   HDC          hdc;
  71.   PAINTSTRUCT  ps;
  72.   RECT         rect;
  73.   short        x, y;
  74.   char   szBuffer[80];
  75.   POINT        point;
  76.     
  77.   switch (message)
  78.     {
  79.     case WM_SIZE:
  80.       cxBlock = LOWORD (lParam) / DIVISIONS;
  81.       cyBlock = HIWORD (lParam) / DIVISIONS;
  82.       return 0;
  83.       
  84.     case WM_SETFOCUS:
  85.       ShowCursor (TRUE);
  86.       return 0;
  87.       
  88.     case WM_KILLFOCUS:
  89.       ShowCursor (FALSE);
  90.       return 0;
  91.       
  92.     case WM_KEYDOWN:
  93.       GetCursorPos (&point);
  94.       ScreenToClient (hwnd, &point);
  95.       
  96.       x = max (0, min (DIVISIONS -1, point.x / cxBlock));
  97.       y = max (0, min (DIVISIONS -1, point.y / cyBlock));
  98.       
  99.       switch (wParam)
  100.         {
  101.         case VK_UP:
  102.           y--;
  103.           break;
  104.           
  105.         case VK_DOWN:
  106.           y++;
  107.           break;
  108.           
  109.         case VK_LEFT:
  110.           x--;
  111.           break;
  112.           
  113.         case VK_RIGHT:
  114.           x++;
  115.           break;
  116.           
  117.         case VK_HOME:
  118.           x = 0;
  119.           y = 0;
  120.           break;
  121.           
  122.         case VK_END:
  123.           x = DIVISIONS - 1;
  124.           y = DIVISIONS - 1;
  125.           break;
  126.           
  127.         case VK_RETURN:
  128.         case VK_SPACE:
  129.           SendMessage (hwnd, WM_LBUTTONDOWN, MK_LBUTTON,
  130.                        MAKELONG (x * cxBlock, y * cyBlock));
  131.           break;
  132.         }
  133.       x = (x + DIVISIONS) % DIVISIONS;
  134.       y = (y + DIVISIONS) % DIVISIONS;
  135.       
  136.       point.x = x * cxBlock + cxBlock / 2;
  137.       point.y = y * cyBlock + cyBlock / 2;
  138.       
  139.       ClientToScreen (hwnd, &point);
  140.       SetCursorPos (point.x,point.y);
  141.       return 0;
  142.       
  143.                     
  144.     case WM_LBUTTONDOWN:
  145.       x = LOWORD (lParam) / cxBlock;
  146.       y = HIWORD (lParam) / cyBlock;
  147.    
  148.  
  149. /*      wsprintf(szBuffer, "cxBlock = %d  cyBlock = %d",cxBlock,cyBlock);
  150.       MessageBox (hwnd, szBuffer, "Block Size", MB_ICONINFORMATION | MB_OK);
  151.       
  152.       wsprintf(szBuffer, "X = %d  Y = %d",x,y);
  153.       
  154.       MessageBox (hwnd, szBuffer, "X Y  -  Size", MB_ICONINFORMATION | MB_OK);
  155.       
  156. */
  157.       
  158.       if (x < DIVISIONS && y < DIVISIONS)
  159.         {
  160.         fState[x][y] ^= 1;
  161.         rect.left    = x * cxBlock;
  162.         rect.top     = y * cyBlock;
  163.         rect.right   = (x + 1) * cxBlock;
  164.         rect.bottom  = (y + 1) * cyBlock;
  165.         
  166.         InvalidateRect (hwnd, &rect, FALSE);
  167.         }
  168.       else
  169.         MessageBeep (0);
  170.       
  171.       return 0;
  172.       
  173.     case WM_PAINT:
  174.       hdc = BeginPaint (hwnd, &ps);
  175.       
  176.       for (x = 0; x < DIVISIONS; x++)
  177.         for (y = 0; y < DIVISIONS; y++)
  178.           {
  179.           Rectangle (hdc, x * cxBlock, y * cyBlock,
  180.                     (x + 1) * cxBlock, (y + 1) * cyBlock);
  181.           if (fState[x][y])
  182.             {
  183.             MoveTo (hdc, x       * cxBlock, y       * cyBlock);
  184.             LineTo (hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
  185.             MoveTo (hdc, x       * cxBlock, (y + 1) * cyBlock);
  186.             LineTo (hdc, (x + 1) * cxBlock, y       * cyBlock);
  187.             }
  188.           }
  189.       EndPaint (hwnd, &ps);
  190.       return 0;
  191.        
  192.     case WM_DESTROY:
  193.       PostQuitMessage (0);
  194.       return 0;
  195.     }
  196.     
  197.   return DefWindowProc (hwnd, message, wParam, lParam);
  198.   }
  199.                   
  200.